#include "FMControl.h"
#include "Ioctl.h"
#include "Wavedev.h"
#include "Reg.h"

CFMControl::CFMControl(void)
{
	
}

CFMControl::~CFMControl(void)
{
	
}

BOOL  CFMControl:: SetFreqency(DWORD nFrenqency)
{
	HANDLE hDev;
	hDev = CreateFile(TEXT("fmc1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_FMC_SET_CUR_FRE, &nFrenqency, sizeof(DWORD),NULL, 0, NULL, NULL);
	}
	else
	{
		return FALSE;
	}
	CloseHandle(hDev);
	return TRUE;
}

DWORD CFMControl:: GetFreqency()
{
	HANDLE hDev;
	DWORD iNewPos;
	hDev = CreateFile(TEXT("fmc1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);

	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_FMC_GET_CUR_FRE, NULL, 0,&iNewPos, sizeof(DWORD), NULL, NULL);
	}
	else
	{
		return 0;
	}	
	
	CloseHandle(hDev);
	return iNewPos;
}

BOOL  CFMControl:: IsOpen()
{	
	BOOL bFmOpen = FALSE;
	CReg reg;
	if(reg.Open(HKEY_CURRENT_USER,L"OEM\\ShellEx"))
	{
		bFmOpen	= reg.GetValueDW(L"FMON",bFmOpen);
	}
	reg.Close();
	return bFmOpen;
}

BOOL  CFMControl:: Open()
{
	HANDLE hDev;
	hDev = CreateFile(TEXT("fmc1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_FMC_POWER_ON, NULL,0,NULL, 0,NULL, NULL);
	}
	else
	{
		return FALSE;
	}
	CloseHandle(hDev);
	waveOutMessage(HWAVEOUT(0), ODM_MSG_SET_SPEAKER_MUTE , FALSE, NULL ); 

	BOOL bFmOpen = TRUE;
	CReg reg;
	if(reg.Open(HKEY_CURRENT_USER,L"OEM\\ShellEx"))
	{
		reg.SetDW(L"FMON",bFmOpen);
	}
	reg.Close();
	return TRUE;
}

BOOL  CFMControl::Close()
{
	HANDLE hDev;
	hDev = CreateFile(TEXT("fmc1:"), GENERIC_READ|GENERIC_WRITE,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL, OPEN_ALWAYS, 0, 0);
	if(hDev)
	{
		DeviceIoControl(hDev, IOCTL_FMC_POWER_DOWN, NULL,0,NULL, 0,NULL, NULL);
	}
	else
	{
		return FALSE;
	}
	CloseHandle(hDev);
	waveOutMessage(HWAVEOUT(0), ODM_MSG_SET_SPEAKER_MUTE , TRUE, NULL ); 
	BOOL bFmOpen = FALSE;
	CReg reg;
	if(reg.Open(HKEY_CURRENT_USER,L"OEM\\ShellEx"))
	{
		reg.SetDW(L"FMON",bFmOpen);
	}
	reg.Close();
	return TRUE;
}